GBML manual / Lists

list_contains

True if any item in target equals value, false otherwise (including when target is empty).

Syntax

list_contains(target, value) -> bool

Arguments

NameType
targetlist
valueelement

Returns

bool

Description

true if any item in target equals value, false otherwise (including when target is empty).

A plain left-to-right search - target does not need to be sorted, and nothing about calling this changes target itself. Useful for "have I already got one of these" checks - a key already collected, a name already on a high-score table - before deciding whether to add another. If you also want to know where the value is, list_find gives its place, or -1 if it is not there.

This is for a list. A stack, a queue and a set each have their own, with the same job: stack_contains, queue_contains and set_contains. Giving one of them to list_contains is refused when the game is checked, and the message names the right one.

Example

/// @type {list<int>}
var scores = [10, 20, 30];
var has20 = list_contains(scores, 20); // true
var has99 = list_contains(scores, 99); // false
A real capture - list_contains(scores, 20), true, the list itself unchangedA real capture - list_contains(scores, 20), true, the list itself unchanged
A real capture - list_contains(scores, 20), true, the list itself unchanged
The same list, list_contains(scores, 99) this time - false, nothing in it matchesThe same list, list_contains(scores, 99) this time - false, nothing in it matches
The same list, list_contains(scores, 99) this time - false, nothing in it matches

On the GBA

A plain left-to-right search (find, the GBA module's own alias for std::find) - O(n) in how many items target holds, same cost as writing the loop by hand. Fine for the small, bounded lists this hardware ever holds; nothing about target needs to be sorted.

See also: list_findlist_addlist_removelist_lengthlist_clearstack_containsqueue_containsset_containsData containers